﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ArkanoidX.Objects;

namespace ArkanoidX.RacketStates
{
    public class NormalState : Racket.State
    {
        RacketStateHelper _stateHelper;

        public NormalState(Game game, Vector2 size, Rectangle regionInSource,
            Rectangle reachableArea)
            : base(game.Content.Load<Texture2D>("racket0"), size, regionInSource, reachableArea)
        {
            // internal state of a normal state is always active
            // since it never expires
            this.InnerState = InternalState.Active;

            _stateHelper = new RacketStateHelper(size);
        }

        public NormalState(Game game, Vector2 size, Rectangle reachableArea)
            : base(game.Content.Load<Texture2D>("racket0"), size, reachableArea)
        {
            this.InnerState = InternalState.Active;

            _stateHelper = new RacketStateHelper(size);
        }

        public NormalState(Game game, Rectangle reachableArea)
            : base(game.Content.Load<Texture2D>("racket0"), reachableArea)
        {
            this.InnerState = InternalState.Active;

            Vector2 _kTargetSize = new Vector2(base.Texture.Bounds.Width, base.Texture.Bounds.Height);
            _stateHelper = new RacketStateHelper(_kTargetSize);
        }

        public override TimeSpan FadingInDuration
        {
            get
            {
                return new TimeSpan(0, 0, 1);
            }
        }

        public override TimeSpan FadingOutDuration
        {
            get
            {
                return new TimeSpan(0, 0, 1);
            }
        }

        public override void Initiate(Racket.State lastState)
        {
            _stateHelper.Initiate(lastState, this);
            base.Initiate(lastState);
        }

        public override void FadingInUpdate(Racket racket, GameTime gameTime, Racket.State fadingOutState)
        {
            _stateHelper.FadingInUpdate(racket, gameTime, fadingOutState, this);
        }

        public virtual void NormalUpdate(Racket racket, GameTime gameTime)
        {
            if (!this.IsActive)
            {
                return;
            }

            KeyboardState keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Left))
            {
                racket.Position.X -= 15;
            }

            if (keyboard.IsKeyDown(Keys.Right))
            {
                racket.Position.X += 15;
            }

            // Keep the racket inside the screen
            if (racket.Position.X < base._reachableArea.Left + base._halfSize.X)
            {
                racket.Position.X = base._reachableArea.Left + base._halfSize.X;
            }

            if (racket.Position.X + base._halfSize.X > base._reachableArea.Right)
            {
                racket.Position.X = base._reachableArea.Right - base._halfSize.X;
            }
        }

        // Do update according to the state of the ship
        public override void Update(Racket racket, GameTime gameTime)
        {
            PreUpdate(racket, gameTime);

            NormalUpdate(racket, gameTime);
        }
    }
}
